function x = do_guido_s(arg)
%
% make a guido structure from our structure (arg).

[s_field, e_field, err] = get_valid_elements(arg);

s = buildeditwindow(arg, [s_field:e_field]);
sn = guido(s, 'Parameter', 1);
if(isempty(sn))
   x = arg;
else
   x = retrievevariables(sn, arg, [s_field:e_field]);
end;
return;


function [s] = buildeditwindow(arg, SEL)

names=fieldnames(arg);
nfield = size(names);
s = [];
for i=min(SEL):max(SEL)
   j = i - min(SEL) + 3;
   thisname = char(names(i));
   thisarg = strcat('arg.', thisname);
   u=eval(char(thisarg));
   if(isstruct(u)) % if data is in a structure, display it appropriately
      if(isempty(u.v))
         s=setfield(s, thisname, ' ');
      elseif(isnumeric(u.v))
         s=setfield(s, thisname, u.v);
      elseif(ischar(u.v))
         s=setfield(s, thisname, u.v); 
      else
         s=setfield(s, thisname, num_2_str(u.v)); 
      end
      
   else % not in a structure! use simple display
%      h1 = text_entry(u, u, u, fw, maxw, x0, t2, id);
   end
end
return;


function [arg_o] = retrievevariables(sn, arg, SEL)

arg_o = arg;
names=fieldnames(arg);
nfield = size(names);
s = sn;
for i=min(SEL):max(SEL)
   j = i - min(SEL) + 3;
   thisname = char(names(i));
   thisarg = strcat('arg.', thisname);
   u=eval(char(thisarg));
   if(isstruct(u)) % if data is in a structure, display it appropriately
      if(isempty(u.v))
         s=getfield(sn, thisname);
         eval(['arg_o.' thisname '.v = s;']);
      elseif(isnumeric(u.v))
         s=getfield(sn, thisname);
         eval(['arg_o.' thisname '.v = s;']);
      elseif(ischar(u.v))
         s=getfield(sn, thisname); 
         eval(['arg_o.' thisname '.v = s;']);
      else
         s=getfield(sn, thisname); 
         eval(['arg_o.' thisname '.v = s;']);
      end
      
   else % not in a structure! use simple display
%      h1 = text_entry(u, u, u, fw, maxw, x0, t2, id);
   end
end
return;


function [s_field, e_field, err] = get_valid_elements(arg)
err = 0;
names=fieldnames(arg);
nfield = length(names);
s_field = strmatch('start', names);
e_field = strmatch('end', names);
if(isempty(s_field) | isempty(e_field))
   QueMessage(sprintf('Edit structure %s lacks start or end field', arg.name));
   err = 1;
   return;
end
s_field=s_field+1;
e_field=e_field-1;
return;
